home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 9577 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  1.0 KB

  1. Path: news.luc.edu!user
  2. From: VArase@varase.it.luc.edu (Verne Arase)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: malloc question
  5. Date: Sun, 10 Mar 1996 22:04:07 -0600
  6. Organization: LUMC
  7. Message-ID: <AD690257966810CEF@mcdialb10.it.luc.edu>
  8. References: <4htonk$350@news.hklink.net>
  9. NNTP-Posting-Host: 147.126.240.126
  10.  
  11. In article <4htonk$350@news.hklink.net>,
  12. alex@station.net (Alex Chu) wrote:
  13.  
  14.  >typedef struct item {
  15.  >  int val;
  16.  >  struct item *next;
  17.  >} ITEM, *PITEM;
  18.  >
  19.  >main()
  20.  >{
  21.  >  PITEM head, current;
  22.  >  head=(PITEM) malloc(sizeof(ITEM));
  23.  >            ^^^^^^^
  24.  >  head->val=1;
  25.  >}
  26.  >
  27.  >I want to know why need to use the type casting PITEM in front of the
  28.  >malloc ?  Please help!
  29.  
  30. If the header was designed for ANSI C, it's probably redundant. An ANSI C
  31. header should define malloc as returning a void *.
  32.  
  33. If it's for a pre-ANSI C, malloc is probably defined as returning a char *.
  34. Therefore, the cast is required to coerce the returned address into head.
  35.  
  36. ---
  37. The above are my own opinions, and not those of my employer.
  38.